package com.dlc.felear.xiaoensale.dao;

import android.content.Context;
import android.util.Log;

import com.dlc.felear.xiaoensale.entity.LocalUser;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LocalUserDao {
    private static final String TAG="LocalUserDao";
    private Dao<LocalUser, Integer> PipeInfoDao;

    /**
     * 构造方法
     * 获得数据库帮助类实例，通过传入Class对象得到相应的Dao
     *
     * @param context
     */
    public LocalUserDao(Context context) {
        try {
            PipeInfoDao = DetailDataOpenHelper.getInstance(context).getDao(LocalUser.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加一条记录
     *
     * @param PipeInfo
     */
    public int update(LocalUser PipeInfo) {
        int temp = -1;
        try {
            Log.e(TAG, "update name=" + JsonHelper.toJson(PipeInfo));
            temp = PipeInfoDao.update(PipeInfo);
        } catch (SQLException e) {
            Log.e(TAG, e.getMessage());
            e.printStackTrace();
        }
        return temp;
    }

    public int add(LocalUser PipeInfo) {
        int temp = -1;
        try {
            Log.e(TAG, "add name=" + JsonHelper.toJson(PipeInfo));
            PipeInfoDao.createOrUpdate(PipeInfo);
        } catch (SQLException e) {
            Log.e(TAG, e.getMessage());
            e.printStackTrace();
        }
        return temp;
    }

    /**
     * 添加多个记录
     *
     * @param list
     */
    public void addAll(List<LocalUser> list) {
        try {
            Log.e(TAG,"生成记录："+JsonHelper.toJson(list));
            for (LocalUser LocalUser : list) {
                if (queryForId(LocalUser.getId()) == null) {
                    add(LocalUser);
                }else {
                    Log.e(TAG,"记录已经存在:"+JsonHelper.toJson(LocalUser));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG,e.getMessage());
        }
    }


    /**
     * 删除一条记录
     *
     * @param PipeInfo
     */
    public void delete(LocalUser PipeInfo) {
        try {
            PipeInfoDao.delete(PipeInfo);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    /**
     * 查询一条记录
     *
     * @param id
     * @return
     */
    public LocalUser queryForId(String id) {
        LocalUser PipeInfo = null;
        try {
            List<LocalUser> tempList = PipeInfoDao.queryBuilder().where().eq("id", id).query();
            if(tempList!=null&&!tempList.isEmpty()){
                PipeInfo=tempList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return PipeInfo;
    }


    /**
     * 查询所有记录
     *
     * @return
     */
    public List<LocalUser> queryForAll() {
        List<LocalUser> PipeInfos = new ArrayList();
        try {
            PipeInfos = PipeInfoDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return PipeInfos;
    }


}
